package com.swq.java.code.chapter17.code06;

import com.swq.java.code.chapter17.code03.Person;

import java.util.*;

public class ColTester {
    public static void main(String[] args) {

        List<Person> persons = new ArrayList<Person>() {
            {  //匿名类初始化代码
                add(new Person("Tom", 21));
                add(new Person("Mike", 32));
                add(new Person("Linda", 19));
                add(new Person("Mary", 29));
            }
        };

        persons.stream()
                .filter(p -> p.getAge() > 20) //过滤条件为年龄大于20
                .forEach(p -> System.out.println(p.getName() + ":" + p.getAge()));

        persons.stream()
                .sorted((p1, p2) -> (p1.getAge() - p2.getAge())) //按照年龄排序
                .limit(3)  //取出三个元素
                .forEach(p -> System.out.println(p.getName() + ":" + p.getAge()));

        int maxAge = persons.parallelStream()
                //把包含Person对象的流映射为保存Person对象的age属性的int类型流
                .mapToInt(p -> p.getAge())
                .max()
                .getAsInt();
        System.out.println("Max Age:" + maxAge);
    }
}